//
//  QKEasyAlbumBottomBar.m
//  EasyIOS
//
//  Created by ChiCo on 2017/5/2.
//  Copyright © 2017年 EasyIOS. All rights reserved.
//

#import "QKEasyAlbumBottomBar.h"
#import <Photos/Photos.h>

@interface QKInnerBanner : UIView

@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UIButton *deleteButton;
@property (nonatomic,strong)PHAsset *asset;

@end

@implementation QKInnerBanner

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 7, 60, 60)];
        [self addSubview:self.imageView];
        self.imageView.layer.cornerRadius = 2;
        [self.imageView setContentMode:UIViewContentModeScaleAspectFill];
        [self.imageView setClipsToBounds:YES];
        [self.imageView setBackgroundColor:[UIColor redColor]];
        
        self.deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.deleteButton setFrame:CGRectMake(53, 0, 14, 14)];
        [self addSubview:self.deleteButton];
        [self.deleteButton setImage:[UIImage imageNamed:@"icon_照片删除"]
                           forState:UIControlStateNormal];
    }
    return self;
}

- (void)setAsset:(PHAsset *)asset
{
    _asset = asset;
    
    @weakify(self);
    [[PHImageManager defaultManager] requestImageForAsset:_asset targetSize:CGSizeMake(120, 120) contentMode:PHImageContentModeAspectFit options:nil resultHandler:^(UIImage * _Nullable result, NSDictionary * _Nullable info)
     {
         @strongify(self);
         [self.imageView setImage:result];
     }];
}
@end

@interface QKEasyAlbumBottomBar ()
@property (nonatomic,strong)UIScrollView *scrollView;
@property (nonatomic,strong)UILabel *numberLabel;
@property (nonatomic,assign)NSUInteger maxNumber;
@end

@implementation QKEasyAlbumBottomBar

- (instancetype)initWithFrame:(CGRect)frame
                 chosedAssets:(NSArray *)chosedAssets
                    maxNumber:(NSUInteger)maxNumber
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setBackgroundColor:[UIColor whiteColor]];
        
        self.scrollView = ({
            UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(10, 8, KScreenWidth-98, 67)];
            [self addSubview:scrollView];
            scrollView;
        });
        
        self.nextButton = ({
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            [self addSubview:button];
            [button setFrame:CGRectMake(KScreenWidth-75, 15, 60, 60)];
            button.layer.cornerRadius = 30;
            button.clipsToBounds = YES;
            [button setBackgroundColor:[UIColor colorWithHexString:@"#ff6600"]];
            
            button;
        });
        
        UILabel *topLabel = [[UILabel alloc] init];
        [self.nextButton addSubview:topLabel];
        [topLabel setText:@"确定"];
        [topLabel setFont:[UIFont systemFontOfSize:14]];
        [topLabel setTextColor:[UIColor whiteColor]];
        [topLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.centerY.mas_equalTo(-7);
        }];
        
        self.numberLabel = ({
            UILabel *label = [[UILabel alloc] init];
            [label setFont:[UIFont systemFontOfSize:14]];
            [label setTextColor:[UIColor whiteColor]];
            [self.nextButton addSubview:label];
            label;
        });
       
        [self.numberLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(0);
            make.centerY.mas_equalTo(7);
        }];
        
        self.maxNumber = maxNumber;
        NSUInteger currentCount = 0;
        if (chosedAssets && [chosedAssets count] > 0) {
            currentCount = [chosedAssets count];
            self.chosedAssets = [NSMutableArray arrayWithArray:chosedAssets];
            for (NSUInteger index = 0; index < currentCount; index++) {
                CGFloat bannerX = index * 70;
                QKInnerBanner *banner = [[QKInnerBanner alloc] initWithFrame:CGRectMake(bannerX, 0, 60, 60)];
                [self.scrollView addSubview:banner];
                banner.asset = [chosedAssets objectAtIndex:index];
                banner.tag = (NSInteger)(1000+index);
            }
            [self.scrollView setContentSize:CGSizeMake(currentCount*70, 0)];
        } else {
             self.chosedAssets = [NSMutableArray array];
        }
            
        [self.numberLabel setText:[NSString stringWithFormat:@"%ld/%ld",currentCount,maxNumber]];
        
    }
    return self;
}


- (void)deleteAsset:(PHAsset *)asset
{
    //找到当前的asset
    NSUInteger tag = [self.chosedAssets indexOfObject:asset];
    [self deletAssetWithIndex:tag];
   
    
}

- (void)deletAssetWithIndex:(NSUInteger)tag
{
    PHAsset *asset = [self.chosedAssets objectAtIndex:tag];
    QKInnerBanner *banner = [self.scrollView viewWithTag:(NSInteger)(1000+tag)];
    CGRect frame = banner.frame;
    [banner removeFromSuperview];
    for (NSUInteger index = tag+1; index < [self.chosedAssets count]; index++) {
        NSUInteger nextTag = (1000+index);
        QKInnerBanner *nextBanner = (QKInnerBanner *)[self.scrollView viewWithTag:(NSInteger)nextTag];
        CGRect tempR = nextBanner.frame;
        [nextBanner setFrame:frame];
        frame = tempR;
        nextBanner.tag = (NSInteger)(nextTag - 1);
    }
    [self.chosedAssets removeObject:asset];
    [self updateChosedNumber];
    if (self.deletAsset) {
        self.deletAsset(asset);
    }
}

- (void)addAsset:(PHAsset *)asset
{
    NSUInteger count = [self.chosedAssets count];
    CGFloat bannerX = count * 70;
    QKInnerBanner *banner = [[QKInnerBanner alloc] initWithFrame:CGRectMake(bannerX, 0, 60, 60)];
    [self.scrollView addSubview:banner];
    banner.asset = asset;
    banner.tag = (NSInteger)(count + 1000);
    [banner.deleteButton addTarget:self
                            action:@selector(deleteAction:)
                  forControlEvents:UIControlEventTouchUpInside];
    [self.chosedAssets addObject:asset];
    [self updateChosedNumber];
    CGFloat start = bannerX - KScreenWidth + 170;
    if (start > 0) {
       [self.scrollView setContentOffset:CGPointMake(start, 0)];
    }
}

- (void)updateChosedNumber
{
    NSUInteger currentCount = [self.chosedAssets count];
    [self.scrollView setContentSize:CGSizeMake(currentCount*70, 0)];
    [self.numberLabel setText:[NSString stringWithFormat:@"%ld/%ld",currentCount,self.maxNumber]];
}

- (void)deleteAction:(UIButton *)button
{
    NSUInteger tag = (NSUInteger)(button.superview.tag - 1000);
    [self deletAssetWithIndex:tag];
}
@end
